package messenger;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginDialog extends JFrame {
	private Logger log = Logger.getLogger("LoginFrame");
	
	private JTextField userComponent = new JTextField(15);
	private JPasswordField pwdComponent = new JPasswordField(15);
	private JTextField serverComponent = new JTextField(15);
	private JCheckBox useProxyComponent = new JCheckBox("Use Proxy");
	private JTextField proxyServerComponent = new JTextField(15);
	private JTextField proxyPortComponent = new JTextField(15);
	private JComboBox proxyTypeComponent = new JComboBox();
	
	private LoginDialog login = null;
	public static final int WIDTH = 240;
	public static final int HEIGHT = 350;
	private String host;
	private int port = 8081;
	public static void main(String[] args) {
		LoginDialog login = new LoginDialog();
		login.setVisible(true);
	}
	
	public LoginDialog() {
		
		Properties config = new Properties();
		try {
			config.load(this.getClass().getResourceAsStream("client.properties"));
			String portStr = config.getProperty("port");
			host = config.getProperty("host");
			if(portStr != null && portStr.trim().length() > 0) {
				try {
					port = Integer.parseInt(portStr);
				} catch (NumberFormatException e) {
					log.info(portStr + " is not a valid port. Default port 8081 will be used");
				}
			}else {
				log.info("Port is not found. Default port 8081 will be used");
			}
		} catch (IOException e1) {
			log.info("File client.properties is not found. Default configuration will be used.");
			e1.printStackTrace();
		}
		
		setSize(WIDTH, HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Messenger");
		setLocation(WIDTH, HEIGHT);
		
		login = this;
		
		JPanel p = new JPanel();
		JLabel userLabel = new JLabel("UserName: ");
		JLabel pwdLabel = new JLabel("Password: ");
		JLabel serverLabel = new JLabel("       Server: ");
		
		p.setLayout(null);
		p.setSize(WIDTH, HEIGHT);
		
		final int H = 25;
		
		serverComponent.setText(host);
		p.add(serverLabel);
		serverLabel.setBounds(20, 10, 80, H);
		p.add(serverComponent);
		serverComponent.setBounds(100, 10, 120, H);
		
		p.add(userLabel);
		userLabel.setBounds(20, 45, 80, H);
		p.add(userComponent);
		userComponent.setBounds(100, 45, 120, H);
		
		p.add(pwdLabel);
		pwdLabel.setBounds(20, 80, 80, H);
		p.add(pwdComponent);
		pwdComponent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
			}
		});
		pwdComponent.setBounds(100, 80, 120, H);
		
		useProxyComponent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JCheckBox useProxyFlag = (JCheckBox)e.getSource();
				if(useProxyFlag.isSelected()) {
					proxyServerComponent.setEditable(true);
					proxyPortComponent.setEditable(true);
					proxyTypeComponent.setEnabled(true);
				}else {
					proxyServerComponent.setEditable(false);
					proxyPortComponent.setEditable(false);
					proxyTypeComponent.setEnabled(false);
				}
			}
		});
		p.add(useProxyComponent);
		useProxyComponent.setBounds(100, 115, 120, H);
		
		JLabel proxyServerLabel = new JLabel("Proxy Server: ");
		p.add(proxyServerLabel);
		proxyServerLabel.setBounds(20, 150, 80, H);
		proxyServerComponent.setEditable(false);
		proxyServerComponent.setText("202.142.26.248");
		p.add(proxyServerComponent);
		proxyServerComponent.setBounds(100, 150, 120, H);
		
		JLabel proxyPortLabel = new JLabel("    Proxy Port: ");
		p.add(proxyPortLabel);
		proxyPortLabel.setBounds(20, 185, 80, H);
		proxyPortComponent.setEditable(false);
		proxyPortComponent.setText("8080");
		p.add(proxyPortComponent);
		proxyPortComponent.setBounds(100, 185, 120, H);
		
		JLabel proxyTypeLabel = new JLabel("   Proxy Type: ");
		p.add(proxyTypeLabel);
		proxyTypeLabel.setBounds(20, 220, 80, H);
		proxyTypeComponent.setEnabled(false);
		proxyTypeComponent.addItem("HTTP");
		proxyTypeComponent.addItem("SOCK");
		proxyTypeComponent.setSelectedItem("HTTP");
		p.add(proxyTypeComponent);
		proxyTypeComponent.setBounds(100, 220, 120, H);
		
		JButton jb = new JButton("Login");
		jb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				login();
			}
		});
		p.add(jb);
		jb.setBounds(150,255, 70, H);
		
		add(p, BorderLayout.CENTER);
	}
	
	public void login() {
		String userName = userComponent.getText();
		String password = new String(pwdComponent.getPassword());
		host = serverComponent.getText();
		if(host == null || host.trim().length() == 0 ||
				userName == null || userName.trim().length() == 0 ||
				password == null || password.trim().length() == 0) {
			JOptionPane.showMessageDialog(login, "Server Username and password coundn't be empty!");
			return;
		}else {
			boolean useProxy = useProxyComponent.isSelected();
			String proxyServer = proxyServerComponent.getText();
			String proxyType = (String)proxyTypeComponent.getSelectedItem();
			int proxyPort = 0;
			if(useProxy) {
				if(proxyServer == null || proxyServer.trim().length() == 0 ||
						proxyType == null || proxyType.trim().length() == 0) {
					JOptionPane.showMessageDialog(login, "Proxy server and Type coundn't be empty!");
					return;
				}
				try {
					proxyPort = Integer.parseInt(proxyPortComponent.getText());
				} catch (NumberFormatException e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(login, "Invalid proxy port!");
					return;
				}
			}
			MessengerClient client = new MessengerClient();
			if(client.connect(host, port, userName, password, useProxy, proxyServer.trim(), proxyPort, proxyType)) {
				MainWindow mainFrame = new MainWindow(client);
				login.setVisible(false);
				mainFrame.setVisible(true);
				login.dispose();
			}else {
				JOptionPane.showMessageDialog(login, "Login failed!");
			}
		}
	}
}
